<?php

namespace app\api\model\wanlshop;

use think\Model;
use traits\model\SoftDelete;

class ShopPayOrder extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'wanlshop_shop_pay_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'createtime_text',
        'paymenttime_text'
    ];

    protected function setOrderNoAttr($value)
    {
        return substr(time(), -8) . substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . $value, -8);
    }

    protected function getStateAttr($value)
    {
        return intval($value);
    }

    public function getPayTypeList()
    {
        return ['balance' => 0, 'wallet' => 4, 'bank' => 5,'qpay' => 6];
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getPaymenttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paymenttime']) ? $data['paymenttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    protected function setPaymenttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 店铺
    public function receiveUserInfo()
    {
        return $this->belongsTo('app\common\model\User', 'receive_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
